<?php
session_start();
$user = isset($_SESSION['user']) ? $_SESSION['user'] : false;
if (!$user) {
    exit(json_encode(array('code' => 1, 'msg' => '请先登录后才能发表')));
}
// 保存博客

$data['uid'] = $user['username'];
$data['title'] = trim($_POST['title']);
$data['cid'] = (int) ($_POST['cid']);
$data['keywords'] = trim($_POST['keywords']);
$data['desc'] = trim($_POST['desc']);
$data['contents'] = htmlspecialchars(trim($_POST['contents']), true); // 用htmlspecialchars去除危险字符比如'
$data['add_time'] = time();
if (!$data['title']) {
    exit(json_encode(array('code' => 1, 'msg' => '标题不能为空')));
}

// 判断有没有输入contents

// 保存数据
require_once $_SERVER['DOCUMENT_ROOT'] . '/lib/Db.php';
$db = new Db();

$id = $db->table('demo_article')->insert($data);
if (!$id) {
    exit(json_encode(array('code' => 1, 'msg' => '保存失败')));
}
exit(json_encode(array('code' => 0, 'msg' => '保存成功')));
